Skip to content

fix(kafka): [Queue Instrumentation 21] Preserve third-party baggage on Kafka producer records#5316

Draft
adinauer wants to merge 1 commit intoref/queue-instrumentation-kafka-log-failuresfrom
fix/queue-instrumentation-kafka-preserve-baggage
Draft

fix(kafka): [Queue Instrumentation 21] Preserve third-party baggage on Kafka producer records#5316
adinauer wants to merge 1 commit intoref/queue-instrumentation-kafka-log-failuresfrom
fix/queue-instrumentation-kafka-preserve-baggage

Conversation

@adinauer
Copy link
Copy Markdown
Member

@adinauer adinauer commented Apr 21, 2026

PR Stack (Queue Instrumentation)


📜 Description

SentryKafkaProducerInterceptor.injectHeaders(...) previously removed and overwrote the outgoing baggage header on every record, discarding any third-party baggage entries already present on the ProducerRecord (e.g. set by another vendor's instrumentation or by the application itself).

injectHeaders(...) now reads the existing baggage header values off the record and passes them to TracingUtils.trace(scopes, existingBaggageHeaders, span). The downstream BaggageHeader.fromBaggageAndOutgoingHeader(...) parses those values, keeps the non-sentry-* entries, and appends them to Sentry's outgoing baggage — so upstream third-party baggage survives while Sentry still owns its own keys.

💡 Motivation and Context

Consistent with how sentry-okhttp, sentry-ktor-client, and the Spring HTTP interceptors already preserve third-party baggage by forwarding existing baggage header values into TracingUtils.traceIfAllowed(...). The raw Kafka producer integration should behave the same so customers don't lose vendor- or app-authored baggage entries when the Sentry producer interceptor writes its header.

💚 How did you test it?

  • Added regression test preserves pre-existing third-party baggage header entries in SentryKafkaProducerInterceptorTest that pre-sets baggage: othervendor=someValue,another=thing on the record, runs onSend, and verifies the resulting outgoing baggage header contains both third-party entries and Sentry entries.
  • ./gradlew :sentry-kafka:test --tests='io.sentry.kafka.SentryKafkaProducerInterceptorTest' — all 6 tests pass.
  • ./gradlew spotlessApply apiDump — clean.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

None.

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

#skip-changelog

…n Kafka producer records

`SentryKafkaProducerInterceptor.injectHeaders(...)` previously removed and
overwrote the outgoing `baggage` header on every record, discarding any
third-party baggage entries already present (e.g. set by another vendor's
instrumentation or the application itself).

Read the existing `baggage` header values off the `ProducerRecord` and
pass them to `TracingUtils.trace(...)`. The downstream
`BaggageHeader.fromBaggageAndOutgoingHeader` preserves non-`sentry-*`
entries in the outgoing header while Sentry continues to own its own
keys.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


This PR will not appear in the changelog.


🤖 This preview updates automatically when you update the PR.

This was referenced Apr 21, 2026
@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 21, 2026

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.37.1 (1) release

⚙️ sentry-android Build Distribution Settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant